import cv2
from PyQt5 import QtCore
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer


class VideoPlayer(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Video Player")

        self.label = QLabel(self)
        layout = QVBoxLayout(self)
        layout.addWidget(self.label)

        self.video_capture = cv2.VideoCapture(r"C:\Users\ChenDong\Desktop\backup\Project\PyCharm\app\answer\导航设计.mp4")
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_frame)
        self.timer.start(33)  # 每隔33毫秒更新一帧

    def update_frame(self):
        ret, frame = self.video_capture.read()
        if ret:
            rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            h, w, ch = rgb_image.shape
            bytes_per_line = ch * w
            qimage = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888)
            pixmap = QPixmap.fromImage(qimage)
            self.label.setPixmap(pixmap.scaled(self.label.size(), aspectRatioMode=QtCore.Qt.KeepAspectRatio))


if __name__ == '__main__':
    app = QApplication([])
    player = VideoPlayer()
    player.show()
    app.exec_()
